/****************************************************************************/
/*  File:       TextUserInteraction.java                                    */
/*  Author:     F. Georges - fgeorges.org                                   */
/*  Date:       2009-06-17                                                  */
/*  Tags:                                                                   */
/*      Copyright (c) 2009 Florent Georges (see end of file.)               */
/* ------------------------------------------------------------------------ */


package org.expath.pkg.saxon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

/**
 * Implementation of {@link UserInteractionStrategy} for text command-line.
 *
 * @author Florent Georges
 * @date   2009-06-17
 */
public class TextUserInteraction
        implements UserInteractionStrategy
{
    public TextUserInteraction()
    {
        this(System.in, System.out, System.err);
    }

    public TextUserInteraction(InputStream in, PrintStream out, PrintStream err)
    {
        myIn = new BufferedReader(new InputStreamReader(in));
        myOut = out;
        myErr = err;
    }

    public void messageInfo(String msg)
    {
        myOut.println(msg);
    }

    public void messageError(String msg)
    {
        myOut.println("[ERR] " + msg);
    }

    public void logInfo(String msg)
    {
        myErr.println(msg);
    }

    public boolean ask(String prompt, boolean dflt)
            throws PackageException
    {
        myOut.print(prompt);
        myOut.print(" [");
        myOut.print(dflt ? 'Y' : 'y');
        myOut.print('/');
        myOut.print(dflt ? 'n' : 'N');
        myOut.print("]: ");
        try {
            String res = myIn.readLine().trim();
            if ( "".equals(res) ) {
                return dflt;
            }
            else if ( "Y".equals(res) || "y".equals(res) ) {
                return true;
            }
            else if ( "N".equals(res) || "n".equals(res) ) {
                return false;
            }
            else {
                throw new PackageException("Incorrect boolean value: " + res);
            }
        }
        catch ( IOException ex ) {
            throw new PackageException("Error reading the user answer", ex);
        }
    }

    public String ask(String prompt, String dflt)
            throws PackageException
    {
        myOut.print(prompt);
        myOut.print(" [");
        myOut.print(dflt);
        myOut.print("]: ");
        try {
            String res = myIn.readLine().trim();
            return "".equals(res)
                    ? dflt
                    : res;
        }
        catch ( IOException ex ) {
            throw new PackageException("Error reading the user answer", ex);
        }
    }

    private BufferedReader myIn;
    private PrintStream myOut;
    private PrintStream myErr;
}


/* ------------------------------------------------------------------------ */
/*  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT.               */
/*                                                                          */
/*  The contents of this file are subject to the Mozilla Public License     */
/*  Version 1.0 (the "License"); you may not use this file except in        */
/*  compliance with the License. You may obtain a copy of the License at    */
/*  http://www.mozilla.org/MPL/.                                            */
/*                                                                          */
/*  Software distributed under the License is distributed on an "AS IS"     */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See    */
/*  the License for the specific language governing rights and limitations  */
/*  under the License.                                                      */
/*                                                                          */
/*  The Original Code is: all this file.                                    */
/*                                                                          */
/*  The Initial Developer of the Original Code is Florent Georges.          */
/*                                                                          */
/*  Contributor(s): none.                                                   */
/* ------------------------------------------------------------------------ */
